<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>[3.7]-扩展类库：微信小程序开发 | PhalApi(π框架) - PHP轻量级开源接口框架 - 助你创造价值！</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">
    <meta content="email=no" name="format-detection">
    <meta name="Author" content="PhalApi,ecitlm,dogstar">
    <meta name="description" content="[3.7]-扩展类库：微信小程序开发，PhalApi是一个PHP轻量级开源接口框架，致力于快速开发接口服务。支持HTTP/SOAP/RPC等协议，可用于搭建接口/微服务/RESTful接口/Web Services。我们不断更新，保持生气；为接口负责，为开源负责！并承诺永久免费！">
    <meta name="keywords" content="PhalApi,phalapi,phalapi接口开发,后台接口开发,后台接口开发框架,接口开发,接口框架,开源接口框架,PHP后台接口开发,PHP接口开发,PHP接口框架,PHP后台接口框架,phalapi官网,PHP接口框架,php接口开发框架,php接口开发,php web框架,π框架,π开发框架,π接口框架,π接口开发框架,api接口,php接口,,PHP接口框架,phalapi文档,phalapi wiki,PhalApi文档,phalapi在线文档,phalapi官方文档">

    <link rel="stylesheet" type="text/css" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" type="text/css" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css" media="screen">
    <link rel="stylesheet" href="../css/main.css" />
    <link rel="stylesheet" href="../css/loading_bar.css" />
    <link rel="icon" href="http://webtools.qiniudn.com/dog_catch.png" type="image/x-icon" />
    <script src="../js/pace.min.js"></script>
	
	<!-- 代码高亮 -->
	<link rel="stylesheet" href="../css/highlight.min.css">
	<script src="../js/highlight.min.js"></script>
	<script>hljs.initHighlightingOnLoad();</script>
    
</head>

<body>
    <!-- navbar start -->
    <div class="navbar navbar-default navbar-fixed-top" id="mainnav" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand website_name" href="/"><!-- <span>PhalApi</span> --><strong></strong></a>
            </div>
            <div class="navbar-collapse collapse" aria-expanded="false" style="height: 1px;">
                <ul class="nav navbar-nav navbar-right" id="nav_bar">
                    <li><a href="https://www.phalapi.net/"><span class="glyphicon glyphicon-home"></span></a></li>
                    <li><a href="https://www.phalapi.net/download.html">下载</a></li>
                    <li class="active"><a href="https://www.phalapi.net/wikis/">文档</a></li>
                    <li><a href="https://www.phalapi.net/donate.html">贡献</a></li>
                    <li><a href="#">|</a></li>
                    <li><a href="http://qa.phalapi.net/" target="_blank">社区</a></li>
                    <li><a href="http://www.ituring.com.cn/book/2405" target="_blank">书籍</a></li>
                    <li><a href="http://demo.phalapi.net/listAllApis.php" target="_blank">体验</a></li>
                    <li><a href="https://github.com/phalapi/phalapi-library" target="_blank">扩展</a></li>
                </ul>
            </div>
            <!--/.nav-collapse -->
        </div>
    </div>
    <!-- navbar end -->


    <div id="content">
        <div class="container">
            <div class="row row-md-flex row-md-flex-wrap">
                <h4><a href="/wikis/%5B3.6%5D-%E6%89%A9%E5%B1%95%E7%B1%BB%E5%BA%93%EF%BC%9A%E6%96%B0%E5%9E%8B%E8%AE%A1%E5%88%92%E4%BB%BB%E5%8A%A1.html">上一章</a>   <a href="/wikis/">文档首页</a>   <a href="/wikis/%5B3.8%5D-%E6%89%A9%E5%B1%95%E7%B1%BB%E5%BA%93%EF%BC%9A%E7%94%A8%E6%88%B7%E3%80%81%E4%BC%9A%E8%AF%9D%E5%92%8C%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95%E9%9B%86%E6%88%90.html">下一章</a></h4>
<hr />
<h2>3.7 扩展类库：基于think-wxminihelper的微信小程序开发扩展</h2>
<p>此扩展基于<a href="https://github.com/wulongtao/think-wxminihelper">think-wxminihelper</a>二次开发实现，并已征得原作者<a href="https://github.com/wulongtao">wulongtao</a>同意。目前，此扩展提供了：  </p>
<ul>
<li>直接可访问的登录接口<code>?service=WechatMini_WXLoginHelper.CheckLogin</code></li>
<li>直接可访问会话检测接口<code>?service=WechatMini_WXLoginHelper.CheckSession</code></li>
<li>在内部获取用户登录态的接口</li>
</ul>
<h2>安装与配置</h2>
<h3>(1) 扩展的安装</h3>
<p>从<a href="http://git.oschina.net/dogstar/PhalApi-Library">PhalApi-Library</a>扩展库中下载获取<strong>WechatMini</strong>扩展包，如使用：</p>
<pre><code class="language-bash">$ git clone https://git.oschina.net/dogstar/PhalApi-Library.git</code></pre>
<p>然后把<strong>WechatMini</strong>目录复制到项目<strong>./PhalApi/Library/</strong>目录下，即：</p>
<pre><code class="language-bash">$ cp ./PhalApi-Library/WechatMini/ ./PhalApi/Library/ -R</code></pre>
<p>到此安装完毕！接下是扩展的配置。</p>
<h3>(2) 扩展的配置</h3>
<p>我们需要在<strong>./Config/app.php</strong>配置文件中追加以下配置： </p>
<pre><code class="language-php">    /**
     * 扩展类库 - 微信小程序
     */
    'WechatMini'  =&gt; array(
        'appid'      =&gt; '', // AppID(小程序ID)
        'secret'     =&gt; '', // AppSecret(小程序密钥)
        'url'        =&gt; 'https://api.weixin.qq.com/sns/jscode2session',
        'grant_type' =&gt; 'authorization_code'
    ),</code></pre>
<p>其中，appid和secret根据自己的小程序配置，相应更新。url和grant_type则不需要更改。  </p>
<h3>(3) 扩展的注册</h3>
<p>可以在初始化文件./Public/init.php或者项目入口文件如./Public/demo/index.php注册此微信小程序扩展。</p>
<pre><code class="language-php">DI()-&gt;wechatMini = new WechatMini_Lite();</code></pre>
<p>这里需要使用显式初始化，WechatMini扩展完成一些内部初始化工作。</p>
<p>注册好后，便可以开始使用了。</p>
<h2>接口服务介绍</h2>
<p>此扩展提供了客户端直接可访问使用的接口服务，主要有：登录接口、会话检测接口。下面分别讲解。  </p>
<h3>(1) 登录接口<code>?service=WechatMini_WXLoginHelper.CheckLogin</code></h3>
<ul>
<li><strong>功能说明</strong>  </li>
</ul>
<p>此接口服务用于小程序初次登录，背后的实现原理参考自<a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html#wxchecksessionobject">微信开放接口文档</a>。</p>
<ul>
<li><strong>接口URI</strong></li>
</ul>
<p>在请求此接口服务时，需要在前面加上接口域名和项目访问路径。而接口服务service名称为：</p>
<pre><code>/?service=WechatMini_WXLoginHelper.CheckLogin</code></pre>
<ul>
<li><strong>接口参数</strong></li>
</ul>
<table class="table table-bordered">
<thead>
<tr>
<th>参数</th>
<th>必须</th>
<th>默认值</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>code</td>
<td>是</td>
<td>无</td>
<td>微信登录凭证</td>
</tr>
</tbody>
</table>
<ul>
<li><strong>返回结果</strong></li>
</ul>
<table class="table table-bordered">
<thead>
<tr>
<th>返回字段</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>code</td>
<td>int</td>
<td>操作状态码，0表示成功，否则表示失败</td>
</tr>
<tr>
<td>openid</td>
<td>string</td>
<td>微信openid，成功时才返回此字段</td>
</tr>
<tr>
<td>session3rd</td>
<td>string</td>
<td>3rd session标识，成功时才返回此字段</td>
</tr>
<tr>
<td>message</td>
<td>string</td>
<td>失败时的提示信息，调试模式下透传微信接口返回的错误信息</td>
</tr>
</tbody>
</table>
<p>例如，成功时会返回：  </p>
<pre><code>{
    "ret": 200,
    "data": {
        "code": 0,
        "openid": "oh1Mb0W63bd6u5rpiJ6eTqrYnYOc",
        "session3rd": "sTyOmVSFKL4h1hKs"
    },
    "msg": ""
}</code></pre>
<p>失败时返回：</p>
<pre><code>{
    "ret": 200,
    "data": {
        "code": -41005,
        "message": "请求Token失败"
    },
    "msg": ""
}</code></pre>
<p>当开启调试模式时，例如secret设置不对时，会返回微信接口返回的错误信息，如：  </p>
<pre><code>{
    "ret": 200,
    "data": {
        "code": 40125,
        "message": "invalid appsecret, view more at http://t.cn/RAEkdVq, hints: [ req_id: TVDXha0288s161 ]"
    },
    "msg": "",
    "debug": {
        ... ...
    }
}</code></pre>
<ul>
<li><strong>小程序使用示例</strong></li>
</ul>
<p>在小程序中，可以这样调用此接口。  </p>
<pre><code class="language-javascript">wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'http://demo.phalapi.net/?service=WechatMini_WXLoginHelper.checkLogin',
            data: {
              code: res.code
            },
            success: function (res) {
              var data = res.data
              if (data.ret == 200 &amp;&amp; data.data.code == 0) {
                console.log("登录成功")
                console.log("openid = " + data.data.openid)
                console.log("session3rd = " + data.data.session3rd)
              } else {
                console.log("登录失败")
                console.log("code = " + data.data.code)
                console.log("message = " + data.data.message)
              }
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });</code></pre>
<h3>(2) 会话检测接口<code>?service=WechatMini_WXLoginHelper.CheckSession</code></h3>
<ul>
<li><strong>功能说明</strong>  </li>
</ul>
<p>在调用登录接口后，接口服务会缓存成功返回的会话信息。这时可使用此会话检测接口判断用户是否已登录且在有效会话期间内。</p>
<ul>
<li><strong>接口URI</strong></li>
</ul>
<p>在请求此接口服务时，需要在前面加上接口域名和项目访问路径。而接口服务service名称为：</p>
<pre><code>/?service=WechatMini_WXLoginHelper.CheckSession</code></pre>
<ul>
<li><strong>接口参数</strong></li>
</ul>
<table class="table table-bordered">
<thead>
<tr>
<th>参数</th>
<th>必须</th>
<th>默认值</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>session3rd</td>
<td>是</td>
<td>无</td>
<td>3rd session标识</td>
</tr>
</tbody>
</table>
<ul>
<li><strong>返回结果</strong></li>
</ul>
<table class="table table-bordered">
<thead>
<tr>
<th>返回字段</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>code</td>
<td>int</td>
<td>操作状态码，0表示成功，否则表示失败</td>
</tr>
<tr>
<td>openid</td>
<td>string</td>
<td>微信openid，成功时才返回此字段</td>
</tr>
<tr>
<td>session3rd</td>
<td>string</td>
<td>3rd session标识，成功时才返回此字段</td>
</tr>
<tr>
<td>message</td>
<td>string</td>
<td>失败时的提示信息，调试模式下透传微信接口返回的错误信息</td>
</tr>
</tbody>
</table>
<p>如成功时返回：</p>
<pre><code>{
  "ret": 200,
  "data": {
    "code": 0,
    "openid": "oh1Mb0W63bd6u5rpiJ6eTqrYnYOc",
    "session3rd": "bV0FBXyjt3YsQxfc"
  },
  "msg": ""
}</code></pre>
<p>会话过期时返回：</p>
<pre><code>{
  "ret": 200,
  "data": {
    "code": -2,
    "message": "登录态已过期"
  },
  "msg": ""
}</code></pre>
<h2>内部获取用户登录态的接口</h2>
<p>在内部，根据session3rd，可以这样获取会话信息。 </p>
<pre><code>$session = DI()-&gt;wechatMini-&gt;getSession($session3rd);</code></pre>
<p>未设置缓存<code>DI()-&gt;cache</code>时返回FALSE，无会话时返回NULL，正常时返回数组：<code>array('openid' =&gt; , 'session_key' =&gt; )</code>。  </p>
<hr />
<h4><a href="/wikis/%5B3.6%5D-%E6%89%A9%E5%B1%95%E7%B1%BB%E5%BA%93%EF%BC%9A%E6%96%B0%E5%9E%8B%E8%AE%A1%E5%88%92%E4%BB%BB%E5%8A%A1.html">上一章</a>   <a href="/wikis/">文档首页</a>   <a href="/wikis/%5B3.8%5D-%E6%89%A9%E5%B1%95%E7%B1%BB%E5%BA%93%EF%BC%9A%E7%94%A8%E6%88%B7%E3%80%81%E4%BC%9A%E8%AF%9D%E5%92%8C%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95%E9%9B%86%E6%88%90.html">下一章</a></h4><div style="float: left">
<h4>
<a href="http://qa.phalapi.net/">还有疑问？欢迎到社区提问！</a>
&nbsp;&nbsp;
<a href="http://docs.phalapi.net/#/v2.0/">切换到PhalApi 2.x 开发文档。</a>
</h4>
</div>
            </div>
        </div>
    </div>

 <!-- 广告位 -->
<div class="grid-wrapper desktop-only">
	<p align="center">
		<a href="http://7xiz2f.com1.z0.glb.clouddn.com/%E6%88%91%E7%9A%84%E5%90%8D%E5%AD%97%E5%8F%AB%EF%BC%9A%CF%80%E6%A1%86%E6%9E%B6%20-%20PhalApi%202016%E5%B9%B4%E5%BA%A6%E5%BC%80%E6%BA%90%E6%80%BB%E7%BB%93%20-%20%E5%AE%98%E6%96%B9%E5%87%BA%E5%93%81.pdf" target="blank"><img width="950" height="100" src="http://7xiz2f.com1.z0.glb.clouddn.com/ad_20170104.png"></a>
		<a href="http://www.itran.cc/" target="blank"><img width="950" height="100" src="http://7xslqv.com1.z0.glb.clouddn.com/images/9/69/b88bc92455dc1239f9a5bf8d72929.png"></a>
		<a href="http://web-tools.phalapi.net/" target="blank"><img width="950" height="100" src="http://7xslqv.com1.z0.glb.clouddn.com/images/7/d0/88f523566c482296aecc43d185ca2.png"></a>
	</p>
</div>

        <!-- footer -->
        <div class="footer">
            <div class="link container ">
                <div class=" col-xs-7 col-sm-7 col-md-7 col-lg-7 ">
                    <h3>PhalApi (π框架)</h3>
                    <p>一个轻量级PHP开源接口框架，专注于接口服务开发，支持HTTP/SOAP/RPC协议，拥有自动生成的在线文档、多种开发语言的客户端SDK包以及可重用的扩展类库，可用于快速搭建微服务、RESTful接口或Web
                        Services。</p>
                    <!-- <p>:</p> -->
                    <a href="https://www.phalapi.net/" target="_blank"> PhalApi </a>
                    <span>|</span>
                    <a href="http://qa.phalapi.net/" target="_blank">社区</a>
                    <span>|</span>
                    <a href="download.html" target="_blank">下载</a>
                    <span>|</span>
                    <a href="/wikis/">文档</a>
                    <span>|</span>
                    <a href="http://www.ituring.com.cn/book/2405" target="_blank">书籍</a>
                    <span>|</span>
                    <a href="http://demo.phalapi.net/listAllApis.php" target="_blank">体验</a>
                    <span>|</span>
                    <a href="donate.html" target="_blank">贡献</a>
                    <span>|</span>
                    <a href="https://github.com/phalapi/phalapi-library" target="_blank">扩展</a>
                    <span>|</span>
                    <a href="/license.html" target="_blank">许可</a>
                    <span>|</span>
                    <a href="/about.html" target="_blank">关于</a>
                    <span>|</span>
                    <a href="https://www.phalapi.net/wikis/en/" target="_blank">English</a>

                </div>
                <div class=" col-xs-5 col-sm-5 col-md-5 col-lg-5 contact_us ">
                    <h3>联系我们</h3>
                    <a href="https://github.com/phalapi/phalapi"><img src="../images/github.png" alt="github" /></a>
                    <a href="http://weibo.com/p/100808d236e99beb645bfb56ed1c37dde9b8bd?k=phalapi%E5%BC%80%E6%BA%90%E6%A1%86%E6%9E%B6&from=501&_from_=huati_topic"><img src="../images/weibo.png"  alt="weibo" /></a>
                    <a href="http://7xslqv.com1.z0.glb.clouddn.com/images/1/b0/894fde762850cb11250253f7d5ede.png"><img src="../images/zfb.png"  alt="zfb" /></a>
					<a target="_blank" href="//shang.qq.com/wpa/qunwpa?idkey=f16292029e59975d4ccfccc70ed87aa5c5646df512e83b0e9e1270e8368be461"><img border="0" src="images/qq.png" alt="π框架 - PhalApi交流群②" title="π框架 - PhalApi交流群②"></a>
					<a href="http://git.oschina.net/dogstar/PhalApi"><img src="../images/git-oschina.png"  alt="git-oschina" /></a>
                </div>
                <div class="cls"></div>
                <div class="text-center copy_right"> ©2015-2017 PhalApi All Rights Reserved. <a href="http://www.miitbeian.gov.cn" target="_blank">粤ICP备15028808号</a>

				<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1255326144'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1255326144%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script>
                </div>
            </div>
        </div>
    </div>


    <a href="https://github.com/phalapi"><img id="gitHub_fllow" style="position: fixed; top: 0; right: 0; border: 0;z-index:9999" src="https://camo.githubusercontent.com/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>


    <script src="../js/jquery-1.11.1.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
</body>

</html>